import api from './axios'

export const login = (credentials) => {
  return api.post('/login', credentials)
}

export const logout = () => {
  return api.post('/logout')
}

export const getDepartment = () => {
  return api.get('/depts'); // 使用 GET 请求
}

export const deleteDepartment = (id) => {
  return api.delete(`/depts?id=${id}`)
}

export const addDepartment = (department) => {
  return api.post('/depts', department)
}

export const updateDepartment = (department) => {
  return api.put('/depts', department)
}

export const indexDepartmentsById = (id) => {
  return api.get(`/depts/${id}`)
}

export const getDepartmentWithParams = (params) => {
  return api.get('/depts/index', { params })
}

//根据id查询员工(登录账户)
export const getEmployeeById = (id) => {
  return api.get(`/emps/${id}`); // 使用 GET 请求
}

//修改员工
export const updateEmployee = (emp) => {
  return api.put("/emps",emp)
}

export const uploadFile = (file) => {
  const formData = new FormData();
  formData.append('file', file.raw); // 'file' 是后端接收字段名，必须和接口一致

  return api.post('/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
};


/**
 * 员工列表分页查询接口
 * @param {Object} params - 查询参数对象
 * @param {string} [params.name] - 姓名
 * @param {number} [params.gender] - 性别 (1:男, 2:女)
 * @param {string} [params.begin] - 开始日期 (格式: yyyy-MM-dd)
 * @param {string} [params.end] - 结束日期 (格式: yyyy-MM-dd)
 * @param {number} params.page - 当前页码
 * @param {number} params.pageSize - 每页条数
 */
export const getEmployees = (params) => {
  return api.get('/emps', {
    params: {
      name: params.name || undefined,
      gender: params.gender || undefined,
      begin: params.begin || undefined,
      end: params.end || undefined,
      page: params.page,
      pageSize: params.pageSize
    }
  })
}

// 获取所有员工（不分页）
export const getAllEmployees = () => {
  return api.get('/emps/list')
}

// 新增员工
export const addEmployee = (employee) => {
  return api.post('/emps', employee)
}

// 删除员工
// 修改后的 deleteEmployee 方法
export const deleteEmployee = (ids) => {
  return api.delete('/emps', {
    params: {
      ids: ids.join(',') // 将 id 数组转换为逗号分隔的字符串
    }
  });
}

//统计员工性别信息
export const getstatisticsGender = () => {
  return api.get('/report/empGenderData')
}

//统计各个职位的员工人数
export const getstatistics = () => {
  return api.get('/report/empJobData')
}

//获取部门总数
export const getDeptTotal = () => {
  return api.get('/report/deptTotal')
}

//获取员工总数
export const getEmpTotal = () => {
  return api.get('/report/empTotal')
}